package Database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Database {
    private static final String oracleDriverName = "oracle.jdbc.driver.OracleDriver";
    private static final String oracleUrlToConnect = "jdbc:oracle:oci8:afcdb/afcdb@AFCDB";
    
    public static Connection getConnection() {
        Connection conn = null;
        try {
            Class.forName(oracleDriverName);
            conn = DriverManager.getConnection(oracleUrlToConnect);
            conn.setAutoCommit(false);
            System.out.println("connection ok");
        } catch (Exception ex) {
            System.out.println("connection error......");
            System.out.println(ex.getMessage());
        }
        return conn;
    }
    
    public double getColumnSum(Connection conn, String sColumn, String sTable, String sWhere){
        String sSQL = "";
        double dNum = 0;
        
        sSQL = "select sum(" + sColumn +")" + 
        " from " + sTable + sWhere;

        try {
            Statement ps = conn.createStatement();
            ResultSet rs = ps.executeQuery(sSQL);
            while (rs.next()){
                dNum = Double.parseDouble(rs.getString(1));
            }
            rs.close();
            ps.close();
        } catch (Exception e) {
            System.out.println(sSQL);
            System.out.println(e.getMessage());
        }
        return dNum;
    }   
}
